import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import {
    queryAliasList,
    queryGardenType,
    queryPropertyList,
    queryAllPropertyList,
    queryAreaList,
    queryAddressList,
    queryPasswordType,
    queryGardenList,
    queryAllOrgList,
    querySelectOrgList,
    queryFranchiseeList,
    queryTariffList,
    queryExpressList,
    queryLogisList,
    queryCheckPerm,
    queryPartnerCompany,
    queryAuthUserList,
    queryShelfList,
    queryCabinetList,
    queryGardenList2,
    queryExpressList2,
    queryProblemTypeList,
    queryAuthCheckPerm,
    queryStreetList,
    queryBizType,
    queryPayType,
    queryDeviceType,
    queryWithdrawReasonList,
    queryOrderImg,
} from '@/services/common';
import { fetchValidation } from '@/utils';

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetchAlias: Effect;
        fetchGardenType: Effect;
        fetchProperty: Effect;
        fetchAllProperty: Effect;
        fetchAreaList: Effect;
        fetchAddressList: Effect;
        fetchPassword: Effect;
        fetchGardenList: Effect;
        fetchAllOrgList: Effect;
        fetchSelectOrgList: Effect;
        fetchFranchiseeList: Effect;
        fetchTariffList: Effect;
        fetchExpressList: Effect;
        fetchLogisList: Effect;
        fetchCheckPerm: Effect;
        fetchPartner: Effect;
        fetchAuthUserList: Effect;
        fetchShelfList: Effect;
        fetchCabinetList: Effect;
        fetchGardenList2: Effect;
        fetchExpressList2: Effect;
        fetchProblemTypeList: Effect;
        fetchAuthCheckPerm: Effect;
        // 新增街道信息
        fetchStreetList: Effect;
        fetchBizType: Effect;
        fetchPayType: Effect;
        fetchDeviceType: Effect;
        fetchWithdrawReasonList: Effect;
        fetchOrderImgUrl: Effect;
    };
    reducers: {
        save: Reducer;
        savePerm: Reducer;
        clear: Reducer;
    };
}

const stateInfo = {
    INITINFO: {
        aliasList: [],
        gardenType: [],
        propertyList: [],
        allPropertyList: [],
        areaList: [],
        addressList: [],
        passwordType: [],
        gardenList: [],
        allOrgList: [],
        selectOrgList: [],
        franchiseeList: [],
        streetList: [],
        tariffList: [],
        expressList: [],
        logisList: [],
        partnerList: [],
        authUserList: {
            count: 0,
            list: [],
        },
        shelfList: [],
        cabinetList: [],
        problemTypeList: [],
        withdrawReasonList: [],
        bizType: {},
        payType: {},
        deviceType: {},
        orderImgUrl: ''
    },
    checkPerm: {
        add: false,
        update: false,
        operOther: false,
    },
};

const Model: ModelType = {
    namespace: 'common',

    state: stateInfo,

    effects: {
        *fetchAlias({ payload }, { call, put }) {
            // 获取公司别名
            const response = yield call(queryAliasList, payload);
            const data = fetchValidation(response);
            if (data) {
                const body = data.body ? Object.entries(data.body) : [];
                yield put({
                    type: 'save',
                    payload: { aliasList: body },
                });
            }
        },

        *fetchGardenType({ payload }, { call, put }) {
            // 获取场景类型[校园，小区]
            const response = yield call(queryGardenType, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { gardenType: data.body || [] },
                });
        },

        *fetchProperty({ payload }, { call, put }) {
            // 获取物业公司
            const response = yield call(queryPropertyList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { propertyList: data.body && data.body.list ? data.body.list : [] },
                });
        },

        *fetchAllProperty({ payload }, { call, put }) {
            // 获取所有物业公司(不分页)
            const response = yield call(queryAllPropertyList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { allPropertyList: data.body || [] },
                });
        },

        *fetchAreaList({ _ }, { call, put }) {
            // 获取省份
            const response = yield call(queryAreaList, { code: '001' });
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { areaList: data.body || [] },
                });
        },

        *fetchAddressList({ payload }, { call, put }) {
            // 获取省市区
            const response = yield call(queryAddressList, payload);
            yield put({
                type: 'save',
                payload: { addressList: response || [] },
            });
        },

        *fetchPassword({ payload }, { call, put }) {
            // 获取密码类型
            const response = yield call(queryPasswordType, payload);
            const data = fetchValidation(response);
            if (data) {
                const body = data.body
                    ? Object.entries(data.body).map((item: any) => ({
                        value: Number(item[0]),
                        label: item[1],
                    }))
                    : [];
                yield data &&
                    put({
                        type: 'save',
                        payload: { passwordType: body },
                    });
            }
        },

        *fetchGardenList({ payload }, { call, put }) {
            // 获取校园小区列表
            const response = yield call(queryGardenList, payload);
            const data = fetchValidation(response);
            if (data) {
                yield data &&
                    put({
                        type: 'save',
                        payload: { gardenList: data.body || [] },
                    });
            }
        },

        *fetchAllOrgList({ payload }, { call, put }) {
            // 获取管理部门
            const response = yield call(queryAllOrgList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { allOrgList: response.body || [] },
                });
        },

        *fetchSelectOrgList({ payload }, { call, put }) {
            // 根据小区获取管理部门
            const response = yield call(querySelectOrgList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { selectOrgList: response.body || [] },
                });
        },

        *fetchFranchiseeList({ payload }, { call, put }) {
            // 获取合作商
            const response = yield call(queryFranchiseeList, payload);
            const data = fetchValidation(response);

            yield data &&
                put({
                    type: 'save',
                    payload: { franchiseeList: Array.isArray(response.body) ? [] : response.body.list },
                });
        },

        *fetchTariffList({ payload }, { call, put }) {
            // 获取收费套餐
            payload.pageIndex = 1;
            payload.pageSize = 20000;
            const response = yield call(queryTariffList, payload);
            yield put({
                type: 'save',
                payload: { tariffList: response.body || [] },
            });
        },

        *fetchExpressList({ payload = {} }, { call, put }) {
            // 获取快递公司
            payload.pageIndex = 1;
            payload.pageSize = 20000;
            payload.status = 1;
            const response = yield call(queryExpressList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { expressList: data.body && data.body.list ? data.body.list : [] },
                });
        },

        *fetchLogisList({ payload }, { call, put }) {
            // 获取物流中心
            const response = yield call(queryLogisList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { logisList: data.body || [] },
                });
        },

        *fetchPartner({ payload }, { call, put }) {
            // 获取合作快递公司列表
            const response = yield call(queryPartnerCompany, payload);
            const data = fetchValidation(response);
            if (data) {
                yield data &&
                    put({
                        type: 'save',
                        payload: { partnerList: data.body || [] },
                    });
            }
        },

        *fetchAuthUserList({ payload }, { call, put }) {
            // 获取合作快递公司列表
            const response = yield call(queryAuthUserList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { authUserList: data.body },
                });
        },

        *fetchCheckPerm({ payload: { name, ...query } }, { call, put }) {
            // 查询菜单权限
            const response = yield call(queryCheckPerm, query);
            const setPayload: any = {
                name,
                status: response.body,
            };
            yield put({
                type: 'savePerm',
                payload: setPayload,
            });
        },

        // 以下为物流中心公共接口

        *fetchShelfList({ payload }, { call, put }) {
            // 获取货架地堆列表
            const response = yield call(queryShelfList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { shelfList: data.body },
                });
        },

        *fetchCabinetList({ payload }, { call, put }) {
            // 获取柜体列表
            const response = yield call(queryCabinetList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { cabinetList: data.body },
                });
        },

        *fetchExpressList2({ payload = {} }, { call, put }) {
            // 获取快递公司
            const response = yield call(queryExpressList2, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { expressList: data.body ? data.body : [] },
                });
        },

        *fetchGardenList2({ payload }, { call, put }) {
            // 获取校园小区列表
            const response = yield call(queryGardenList2, payload);
            const data = fetchValidation(response);
            if (data) {
                yield data &&
                    put({
                        type: 'save',
                        payload: { gardenList: data.body || [] },
                    });
            }
        },

        *fetchProblemTypeList({ payload }, { call, put }) {
            // 获取问题件类型列表
            const response = yield call(queryProblemTypeList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { problemTypeList: data.body },
                });
        },

        *fetchAuthCheckPerm({ payload }, { call, put }) {
            // 验证操作权限
            const response = yield call(queryAuthCheckPerm, payload);
            yield put({
                type: 'savePerm',
                payload: {
                    name: payload.oper,
                    status: response.body,
                },
            });
        },

        *fetchStreetList({ payload }, { call, put }) {
            // payload
            const response = yield call(queryStreetList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { streetList: response.body ? response.body : [] },
                });
        },

        // 获取业务类型
        *fetchBizType({ payload }, { call, put }) {
            const response = yield call(queryBizType, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { bizType: response.body ? response.body.biz_type : {} },
                });
        },

        // 获取用户类型
        *fetchPayType({ payload }, { call, put }) {
            const response = yield call(queryPayType, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { payType: response.body ? response.body.biz_type : {} },
                });
        },

        // 获取设备类型
        *fetchDeviceType({ payload }, { call, put }) {
            const response = yield call(queryDeviceType, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { deviceType: response.body ? response.body.biz_type : {} },
                });
        },

        // 
        *fetchWithdrawReasonList({ payload }, { call, put }) {
            // 获取退回原因列表
            const response = yield call(queryWithdrawReasonList, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { withdrawReasonList: data.body },
                });
        },

        // 获取快递单图片
        *fetchOrderImgUrl({ payload }, { call, put }) {
            // 获取退回原因列表
            const response = yield call(queryOrderImg, payload);
            const data = fetchValidation(response);
            yield data &&
                put({
                    type: 'save',
                    payload: { orderImgUrl: data.body },
                });
        },
    },

    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                INITINFO: {
                    ...state.INITINFO,
                    ...payload,
                },
            };
        },

        savePerm(state, { payload }) {
            const check = { ...state.checkPerm };
            check[payload.name] = payload.status;
            return {
                ...state,
                checkPerm: check,
            };
        },

        clear() {
            return stateInfo;
        },
    },
};

export default Model;
